package ctrs.controller;

import com.alibaba.fastjson.JSON;
import ctrs.bean.Note;
import ctrs.bean.QueryInfo;
import ctrs.dao.NoteDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author Akie
 * @create 2021-03-11 10:45
 */
@RestController
public class NoteController {

    @Autowired
    NoteDao noteDao;

    @CrossOrigin
    @RequestMapping("/allNote")
    public String getAllNote(QueryInfo queryInfo){
        System.out.println(queryInfo);
        List<Note> note = noteDao.getAllNote("%"+queryInfo.getQuery()+"%");
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",note);
        System.out.println("res:"+res);
        String note_json = JSON.toJSONString(res);
        return note_json;
    }

    @RequestMapping("/addNote")
    public String addNote(@RequestBody Note note){
        int i = noteDao.addNote(note);
        return i>0?"success":"error";
    }

}
